<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <script>
      function * foo(){
          console.log('start');
          yield 'foo';
      }

      const ge = foo();
      const rl = ge.next();
      console.log(rl)

      function isBgTo100(num){
        return new Promise((resolve,reject)=>{
          setTimeout(()=>{
            if(num>100){
              resolve(num);
            }else{
              reject(new Error('less than 100'))
            }
          },2000)
        //
        
        })
      }

      function * main() {
          console.log('start');
          try{
            const rl = yield isBgTo100(101);
            console.log(rl)
            const rl2 = yield isBgTo100(102);
            console.log(rl2)
            const rl3 = yield isBgTo100(103);
            console.log(rl3)
          }catch(e){
              console.log(e);
          }
          
      }

      function runMain(rl){
        if(rl.done) {
            console.log('end');
            return;
        }
        rl.value.then(data=>{
            runMain(g.next(data));
        },err=>{
            g.throw(err);
        })
        // const _rl=gen.next();
        // _rl.value.then(data=>{ 
        //     const _nextRl=gen.next();
        //     if(_nextRl.done){return;}
        //     runMain(gen);
        // })
        
      }

      const g = main();
      runMain(g.next());

    //   const g = main();
    //   const rl3 = g.next();

    //   rl3.value.then(data=>{
    //       const rl4=g.next(data)
    //       rl4.value.then(data=>{
    //         g.next(data)
    //       })
    //   })

    </script>
  </body>
</html>
